{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\" style=\"margin-top: 1em;\"><ul class=\"toc-item\"><li><span><a href=\"#Intro\" data-toc-modified-id=\"Intro-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Intro</a></span></li><li><span><a href=\"#Flocks\" data-toc-modified-id=\"Flocks-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Flocks</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Intro\n",
    "Exploratory notebook related to Reactive Agents and Multiagents Systems. Includes toy implementations and visualization of the major introductory concepts for the topic. Content is strongly based and driven by [Kadenze Online Course](https://www.kadenze.com/courses/generative-art-and-computational-creativity-i).\n",
    "\n",
    "A **software (or digital) agent** is a computer program that acts autonomously (or semi-autonomously), and executes actions based on external and internal inputs.\n",
    "\n",
    "**Deliberative (or cognitive) agents** maintain an internal state which influence their behavior.\n",
    "\n",
    "**Reactive agents** don't maintain an internal state and act on external inputs in a reflexive manner, based on predefined behavioral sets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Basic libraries import\n",
    "import time\n",
    "import numpy as np\n",
    "import pdb\n",
    "import sys\n",
    "import os\n",
    "from pathlib import Path\n",
    "import seaborn as sns\n",
    "from collections import namedtuple\n",
    "\n",
    "# Project specific libraries\n",
    "from scipy.spatial import distance\n",
    "from PIL import Image, ImageDraw\n",
    "\n",
    "# Plotting\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import animation\n",
    "\n",
    "sns.set_style(\"dark\")\n",
    "sns.set_context(\"paper\")\n",
    "\n",
    "%matplotlib notebook\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Flocks\n",
    "[“Flocks, Herds, and Schools: A Distributed Behavioral Model” - Craig Reynolds 1987 SIGGRAPH paper](https://team.inria.fr/imagine/files/2014/10/flocks-hers-and-schools.pdf). Here he referred to simulated flocks as *boids*.\n",
    "\n",
    "Is about the behavior/movements simulation for a group of entities/units. Ideally there is no leader, and the flock behavior emerges from three simple rules:\n",
    "* **Cohesion** unit tendency to the average position of its neighbors\n",
    "* **Alignment** unit tendency to align itself with the average heading of its neighbors\n",
    "* **Separation** unit avoidance of collisions with its neighbors\n",
    "\n",
    "*Neighbors* are only those entities of the group that a unit can perceive. This are defined based on a unit visibility.\n",
    "\n",
    "Initially we will treat units as particles, so there is no need to take care of the rotation aspect of a unit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import locally defined class\n",
    "from Flock import Flock\n",
    "CIRCLE_RADIUS = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw_flow(flock: Flock, draw, circle_radius:int=CIRCLE_RADIUS, \n",
    "              visibility_radius=Flock.VISIBILITY_RADIUS):\n",
    "    for unit in flock.units:\n",
    "        x, y = unit.pos\n",
    "        x_v, y_v = unit.vel\n",
    "        # draw entity\n",
    "        draw.ellipse([x-circle_radius, y-circle_radius, x+circle_radius, y+circle_radius], \n",
    "                     fill='black', outline='black')\n",
    "        # draw visibility\n",
    "        draw.ellipse([x-visibility_radius, y-visibility_radius, \n",
    "                      x+visibility_radius, y+visibility_radius], \n",
    "                     outline='red')\n",
    "        # draw direction\n",
    "        draw.line([x, y, (x+x_v*30), (y+y_v*30)], fill='blue')\n",
    "        # draw velocity\n",
    "        #draw.line([x, y, x+x_v, y+y_v], fill='blue')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Flock import Flock\n",
    "img_size = 1000\n",
    "flock = Flock(10, canvas_size=img_size/2)\n",
    "img = Image.new('RGB', (img_size, img_size), (255, 255, 255))\n",
    "fig, ax = plt.subplots(dpi=120, figsize=(5, 5))\n",
    "im = ax.imshow(img)\n",
    "plt.axis('off')\n",
    "\n",
    "def animate(i, flock, img_size):\n",
    "    img = Image.new('RGB', (img_size, img_size), (255, 255, 255))\n",
    "    draw = ImageDraw.Draw(img)\n",
    "    flock.update()\n",
    "    draw_flow(flock, draw)\n",
    "    im.set_data(img)\n",
    "\n",
    "ani = animation.FuncAnimation(fig, animate, frames=500, interval=100, fargs=[flock, img_size])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:image-processing]",
   "language": "python",
   "name": "conda-env-image-processing-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
